<template>
  <div>
    {{author}}
      <div>
        <p>图书名称:<el-input v-model="obj.bookname" style="width: 240px" placeholder="图书名称" /></p>
        <p>图书价格:<el-input v-model="obj.price" style="width: 240px" placeholder="图书价格" /></p>
        <p>图书库存:<el-input v-model="obj.number" style="width: 240px" placeholder="图书库存" /></p>
        <p>图书作者:
          <el-select v-model="obj.author" placeholder="作者" style="width: 240px">
              <el-option
                v-for="(item,key) in author"
                :key="key"
                :label="item.name"
                :value="item.id"
              />
          </el-select>

        </p>
        <p><el-button type="primary" @click="add">添加</el-button></p>
      </div>
      <hr>
      <p>
        <el-input v-model="query.name" style="width: 240px" placeholder="图书名称" />
        <el-input v-model="query.num" style="width: 240px" placeholder="图书库存" />
        <el-select v-model="query.author" placeholder="作者" style="width: 240px">
              <el-option
                v-for="(item,key) in author"
                :key="key"
                :label="item.name"
                :value="item.id"
              />
          </el-select>
        <el-button @click="search">搜索</el-button>
      </p>
      <el-table :data="booklist" border style="width: 100%">
        <el-table-column prop="bookname" label="图书名称" width="180" />
        <el-table-column prop="price" label="图书价格" width="180" />
        <el-table-column prop="author" label="作者" />
        <el-table-column  label="是否展示" >
          <template #default="scope">
            <el-switch v-model="scope.row.is_show" @change="changestate(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column  label="操作" >
          <template #default="scope">
            <el-button type="primary" @click="deldata(scope.row.id)">删除</el-button>
            <el-button type="success" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination 
      background 
      layout="prev, pager, next" 
      :total="total" 
      :page-size="query.size"
      :current-page="query.page"
      @current-change="change"  
      />
    <!-- 编辑的对话框   -->
      <el-dialog
        v-model="flag"
        title="编辑"
        width="60%"
      >
        <div>
          {{detail.author}}
        <p>图书名称:<el-input v-model="detail.bookname" style="width: 240px" placeholder="图书名称" /></p>
        <p>图书价格:<el-input v-model="detail.price" style="width: 240px" placeholder="图书价格" /></p>
        <p>图书库存:<el-input v-model="detail.number" style="width: 240px" placeholder="图书库存" /></p>
        <p>图书作者:
          <el-select v-model="detail.author" placeholder="作者" style="width: 240px">
              <el-option
                v-for="(item,key) in author"
                :key="key"
                :label="item.name"
                :value="item.id"
              />
          </el-select>

        </p>
      </div>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="flag = false">取消</el-button>
            <el-button type="primary" @click="save">
              保存
            </el-button>
          </div>
        </template>
    </el-dialog>
  </div>
</template>
<script setup>
//导包
import {ref,onMounted} from "vue"
import axios from "axios"
import {ElMessage} from "element-plus"

//设置查询参数
let query=ref({
  page:1,
  size:3,
  name:"",
  num:"",
  author:""
})
//设置一个变量 来存储列表 和总的记录数
let booklist=ref([])
let total=ref(null)

//写一个函数来去执行接口
const  getdata=()=>{
  axios.get("http://127.0.0.1:8000/book/book/",{"params":query.value})
  .then(res=>{
    console.log(res)
    booklist.value=res.data.results
    total.value=res.data.count
  })
}

const change=(page)=>{
  query.value.page=page
  getdata()
}

//关于添加
let obj=ref({
  "bookname":"",
  "price":"",
  "number":"",
  "author":""
})

let author=ref([])

//获取所有作者
const getauthor=()=>{
  axios.get("http://127.0.0.1:8000/book/author/").then(res=>{
    author.value=res.data
  })
}

const add=()=>{

  axios.post("http://127.0.0.1:8000/book/book/",obj.value).then(res=>{
    if(res.data){
      //添加成功  清空表单  提醒  重新请求
      obj.value={
        "bookname":"",
        "price":"",
        "number":"",
        "author":""
      }
      alert("添加成功")
      getdata()
    }
  })
}


//关于删除
const deldata=(id)=>{
  axios.delete(`http://127.0.0.1:8000/book/book/${id}/`).then(res=>{
    if(res.status==204){
      ElMessage.success("删除成功")
      //展示第一页
      query.value.page=1
      getdata()
    }
  })
}

//关于修改状态
const changestate=(info)=>{
  axios.put(`http://127.0.0.1:8000/book/book/${info.id}/changestate/`,{"show":info.is_show}).then(res=>{
    if(res.status==200){
      ElMessage.success(res.data.msg)
    }
  })
}
//关于搜索
const search=()=>{
  getdata()
}

//关于编辑
let flag=ref(false)
//用来保存当前行的数据
let detail=ref({})
const edit=(info)=>{
  //显示弹框
  flag.value=true
  detail.value=info
}
//数据的保存
const save=()=>{
  //console.log(detail.value)
  //走接口
  axios.put(`http://127.0.0.1:8000/book/book/${detail.value.id}/`,detail.value).then(res=>{
    if(res.data){
      //关框
      flag.value=false
      ElMessage.success("修改成功")
    }
  })
}


onMounted(()=>{
  getdata()
  getauthor()
})


</script>
<style>
.el-pagination{
  margin-top:30px;
}
</style>
